package com.sz.common.core.service;

import com.sz.common.base.exception.CommonErrorCodes;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.logger.Logger;

import com.sz.common.base.utils.BeanUtils;
import com.sz.common.core.service.role.RoleMembership;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Locale;

/**
 * Function: 获取当前账户下的详细信息 <br>
 * Author: Charles <br>
 * Date: 2016-11-03 17:43:00
 */
public class PrincipalUtils {
    public static final char SPLI = '-';
    public static final String SPLITOR = "-";
    public static final int SPLIT_LEN = 2;
    private static Logger logger = Logger.getLogger(PrincipalUtils.class);
    private static PrincipalManager principalManager;


    /**
     * @return
     */
    public static PrincipalManager getPrincipalManager() {
        if (principalManager == null) {
            principalManager = BeanUtils.getBean(PrincipalManager.class);
            if (principalManager == null) {
                throw new RuntimeException("cannot find instance of com.sz.common.core.service.PrincipalManager");
            }
        }
        return principalManager;
    }

    /**
     * @return
     */
    public static Principal tryToGetPrincipal() {
        return getPrincipalManager().getPrincipal();
    }

    public static Principal getPrincipal() {
        Principal principal = tryToGetPrincipal();
        if (principal == null) {
            throw Exceptions.bizException(CommonErrorCodes.ERROR_401_MSG);
        }
        return principal;
    }

    /**
     * 获取域Id.
     *
     * @return 域Id
     */
    public static int getDomainId() {
        return getPrincipal().getDomainId();
    }

    /**
     * 获取账户登录名，可能为运营账户，供应商，或者客户.
     *
     * @return 账户登录名
     */
    public static int getAccountId() {
        return getPrincipal().getAccountId();
    }

    /**
     * 获取账户Id，可能为运营账户，供应商，或者客户.
     *
     * @return 账户Id
     */
    public static String getAccountName() {
        return getPrincipal().getAccountName();
    }


    /**
     * 判断是否是ROOT账户.
     *
     * @return 是否是ROOT账户
     */
    public static boolean isRoot() {
        return getPrincipal().isRoot();
    }

    /**
     * 判断是否具备管理权限
     *
     * @return
     */
    public static boolean isAdmin() {
        return getPrincipal().isAdmin();
    }

    /**
     * 获取当前账户的角色Id列表.
     *
     * @return 角色Id列表
     */
    public static List<Integer> getRoleIds() {
        return getPrincipal().getRoleIds();
    }

    /**
     * 获取当前账户的角色code列表.
     *
     * @return 角色code列表
     */
    public static List<String> getRoles() {
        return getPrincipal().getRoles();
    }

    /**
     * 获取当前账户下的权限Id列表.
     *
     * @return 权限Id列表
     */
    public static List<Integer> getPermissionIds() {
        return getPrincipal().getPermissionIds();
    }

    /**
     * 获取当前账户下的权限列表.
     *
     * @return 权限编码列表
     */
    public static List<String> getPermissions() {
        return getPrincipal().getPermissions();
    }

    /**
     * 获取账户设置的默认语言环境.
     *
     * @return 账户登录名
     */
    public static String getLocalLanguage() {
        String language;
       try {
           language= getPrincipal().getPreferredLanguage();
       }catch (Exception exception){
           language="zh-cn";
       }
        return language;
    }

    /**
     * @return
     */
    public static Locale getLocal() {
        String defaultLang = "zh-CN";
        String lang = "";
        Principal principal = tryToGetPrincipal();
        if (principal != null) {
            lang = principal.getPreferredLanguage();
        }
        lang = StringUtils.defaultIfEmpty(lang, defaultLang);
        if (lang.indexOf(SPLI) > 0) {
            String[] segs = lang.split(SPLITOR);
            if (segs.length == SPLIT_LEN) {
                return new Locale(segs[0], segs[1]);
            }
        }
        return new Locale(lang);
    }

    /**
     * 获取登录IP地址.
     *
     * @return 登录IP地址
     */
    public static String getIPAddress() {
        return getPrincipal().getIpAddr();
    }

    /**
     * 获取principal的成员类型
     *
     * @return
     */
    public static RoleMembership getRoleMembership() {
        return new RoleMembership(getPrincipal().getAccountId(), getPrincipal().getMembershipType());
    }
}
